package com.webside.Advertisingstatistics.service.impl;

/***
 * lpz
 */
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webside.Advertisingstatistics.mapper.AdDailyStatMapper;
import com.webside.Advertisingstatistics.model.AdDailyStatEntity;
import com.webside.Advertisingstatistics.model.AdbrowerRecordsEntity;
import com.webside.Advertisingstatistics.service.AdDailyStatService;
import com.webside.Advertisingstatistics.service.AdbrowerRecordsService;
import com.webside.base.baseservice.impl.AbstractService;
import com.webside.resource.model.ResourceEntity;

@Service("AdDailyStatService")
public class AdDailyStatServiceImpl extends AbstractService<ResourceEntity, Long> implements AdDailyStatService {
	@Autowired
	private AdDailyStatMapper adDailyStatMapper;
	@Autowired
	private AdbrowerRecordsService adbrowerRecordsService;
	// 时间格式
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	// 这句必须要加上。不然会报空指针异常，因为在实际调用的时候不是BaseMapper调用，而是具体的mapper，这里为userMapper

	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(adDailyStatMapper);
	}

	// 在每日00:01分的时候将昨日的数据统计出来并存入mysql中
	@Override
	public int addadDailyStat() {
		// TODO Auto-generated method stub
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		java.util.Date yesterday = cal.getTime();
		// 在今天的凌晨00：01统计昨天的广告浏览数据
		AdbrowerRecordsEntity adbrowerRecords = adbrowerRecordsService.selectSumCollectStat("", yesterday);
		AdDailyStatEntity adDailyStat = new AdDailyStatEntity();
		// 为要插入数据库的实体类添加数据
		// 总曝光秒数
		adDailyStat.setExposureCount(adbrowerRecords.getExposureCount());
		// 总点击次数
		adDailyStat.setPv(adbrowerRecords.getClickCount());
		// 总独立访客
		adDailyStat.setUv(adbrowerRecords.getuVisitorsCount());
		// 总ip
		adDailyStat.setIp(adbrowerRecords.getIpCount());
		// 统计数据的时间
		adDailyStat.setStatDate(yesterday);
		return adDailyStatMapper.addadDailyStat(adDailyStat);
	}

	// 查询近90日平均的广告总曝光量，总点击量，总独立访客总Ip数
	@Override
	public AdDailyStatEntity avgAdDailyStatCount() {
		// TODO Auto-generated method stub

		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		// java.util.Date newTime = cal.getTime();
		String newTime = sdf.format(cal.getTime());
		// 获取截止于昨日90日之前的日期
		Calendar cal2 = Calendar.getInstance();
		cal2.add(Calendar.DATE, -90);
		String oldTime = sdf.format(cal2.getTime());
		
		//后台数据不满90条所以算总和在/90
		return adDailyStatMapper.sumAvgAdDailyStatCount(newTime, oldTime);
		//return adDailyStatMapper.avgAdDailyStatCount(newTime, oldTime);

	}

	// 查询历史广告总曝光量，总点击量，总独立访客总Ip数的峰值
	@Override
	public AdDailyStatEntity maxAdDailyStatCount() {
		// TODO Auto-generated method stub
		return adDailyStatMapper.maxAdDailyStatCount();
	}

	// 查询历史广告总曝光量，总点击量，总独立访客总Ip数的累计
	@Override
	public AdDailyStatEntity sumAdDailyStatCount() {
		// TODO Auto-generated method stub
		return adDailyStatMapper.sumAdDailyStatCount();
	}

	// 查询昨日数据
	@Override
	public AdDailyStatEntity selectYesterDayAll() {
		// TODO Auto-generated method stub
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		// java.util.Date newTime = cal.getTime();
		String yesterDay = sdf.format(cal.getTime());
		return adDailyStatMapper.selectYesterDayAll(yesterDay);
	}

}
